package com.example.swd1.gudong.main.sportrang;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.constant.Constant;
import com.example.swd1.gudong.main.MainActivity;

import java.util.ArrayList;
import java.util.List;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class SportRangFragment extends Fragment implements ViewPager.OnPageChangeListener {
    private ViewPager sportViewPager;
    private SportRangFragmentDynamic sportRangFragmentDynamic;
    private SportRangFragmentSquare sportRangFragmentSquare;
    private List<Fragment> fragmentList;
    private SportRangViewPageAdapter adapter;
    private MainActivity activity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (MainActivity) getActivity();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sport_rang,container,false);
        sportViewPager = (ViewPager) view.findViewById(R.id.sport_rang_view_page);

        sportRangFragmentDynamic = new SportRangFragmentDynamic();
        sportRangFragmentSquare = new SportRangFragmentSquare();
        fragmentList = new ArrayList<>();
        fragmentList.add(sportRangFragmentDynamic);
        fragmentList.add(sportRangFragmentSquare);
        adapter = new SportRangViewPageAdapter(getActivity().getSupportFragmentManager(),fragmentList);
        sportViewPager.setAdapter(adapter);
        sportViewPager.setOnPageChangeListener(this);
        return view;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        //change activity title and menu
        activity.setCurrentFragmentInSportRang(position+Constant.VIEW_PAGE_FLAG_DYNAMIC);
        switch (position){
            case 0:
                activity.changeTitleAndMenu(Constant.VIEW_PAGE_FLAG_DYNAMIC);
                break;
            case 1:
                activity.changeTitleAndMenu(Constant.VIEW_PAGE_FLAG_SQUARE);
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
